#!/usr/bin/env python3
# ros-test-mecs.py
# MEC Server: Subscriber to /mec/test_topic

import rclpy
from rclpy.node import Node
from std_msgs.msg import String
import time
import os

class MecTestSubscriber(Node):
    def __init__(self):
        super().__init__('mec_test_subscriber')
        self.subscription = self.create_subscription(
            String,
            '/mec/test_topic',
            self.listener_callback,
            10)
        self.subscription  # prevent unused variable warning
        self.get_logger().info('MEC Subscriber started. Waiting for messages...')

    def listener_callback(self, msg):
        recv_time = time.time()
        self.get_logger().info(f'Received: "{msg.data}" at {recv_time:.6f}')

def main(args=None):
    rclpy.init(args=args)

    # Set ROS_DOMAIN_ID from environment (for isolation)
    domain_id = os.getenv('ROS_DOMAIN_ID', '42')
    os.environ['ROS_DOMAIN_ID'] = str(domain_id)

    mec_subscriber = MecTestSubscriber()

    try:
        rclpy.spin(mec_subscriber)
    except KeyboardInterrupt:
        mec_subscriber.get_logger().info('Shutting down MEC subscriber...')
    finally:
        mec_subscriber.destroy_node()
        rclpy.shutdown()

if __name__ == '__main__':
    main()